/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示输出调制后的38K红外调制信号
  *          通过UART2_TXD 与 ATIM_CH1 “逻辑与” 调制出波形
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J13、J28
  *                    短接J27
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_hal_irmod.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;
UART_HandleTypeDef huart2;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void ATIM_Init(void);
static void UART_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    // 初始化ATIM
    ATIM_Init();

    // 初始化UART2
    UART_Init();

    // 初始化IR所用到的管脚
    Bsp_IrPort_Init();

    // 配置IR模式
    HAL_IRMOD_Config(IRMOD_UART2TXD_AND_ATIMCH1_AND_IRSW, IRMOD_POLARITY_POSITIVE);
    HAL_IRMOD_IRSW_SETHIGH();                                         // 设置IRSW为1

    // 请从PB02及PA03观察输出的TXD信号及红外调制信号
    while (1)
    {
        HAL_UART_SendByte(huart2.Instance, HAL_GetTick());
        HAL_Delay(100);
    }
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    //------------------------------------------------------------
    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 1 - 1;
    hatim.Init.Period            = 1263 - 1;                          // 频率 = 48MHz / 1263 = 38K
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    hatim.OcInit.OCMode       = ATIM_OCMODE_PWM1;
    hatim.OcInit.Pulse        = 379 - 1;                              // 379 / 1263 = 30%
    hatim.OcInit.OCPolarity   = ATIM_OCPOLARITY_HIGH;
    hatim.OcInit.OCFastMode   = ATIM_OCFAST_DISABLE;
    hatim.OcInit.OCIdleState  = ATIM_OCIDLESTATE_RESET;
    HAL_ATIM_OC_ConfigChannel(&hatim, &hatim.OcInit, ATIM_CHANNEL_1);

    HAL_ATIM_OC_Start(&hatim, ATIM_CHANNEL_1);
}


/*******************************************************************************
 * @brief  UART初始化
 * @param  None
 * @retval None
 */
static void UART_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    // 外设时钟使能
    BSP_UART_RXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_TXD_PORT_PERIPH_CLK_ENABLE();
    BSP_UART_PERIPH_CLK_ENABLE();

    // 配置UART TXD管脚
    BSP_UART_TXD_AF();
    GPIO_InitStruct.Pins = BSP_UART_TXD_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(BSP_UART_TXD_PORT, &GPIO_InitStruct);

    // 配置 UART
    MemClr(&huart2, sizeof(huart2));

    huart2.Instance         = SYM_UART2;
    huart2.Init.ClockSource = UART_CLKSRC_PCLK;
    huart2.Init.PclkFreq    = 48000000;
    huart2.Init.BaudRate    = 2400;                                   // Baud = 2400
    huart2.Init.ParityMode  = UART_PARITYMODE_NONE;                   // 无校验位
    huart2.Init.StopBits    = UART_STOPBITS_1;                        // STOP = 1
    huart2.Init.WorkMode    = UART_WORKMODE_ASYN;
    huart2.Init.Polarity    = UART_POLTX_NEGATIVE;
    HAL_UART_Init(&huart2);                                           // 初始化UART

    HAL_UART_TXD_Enable(huart2.Instance);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
